package com.manager.car.action;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.manager.car.po.Cars;
import com.manager.car.po.NewsInfo;
import com.manager.car.service.ICarsService;
import com.manager.car.util.PageModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CarsAction extends ActionSupport{

	private ICarsService dao;
	private int id;
	private Cars car;
	private PageModel<Cars> pageModel;
	private int page,limit;
	
	public String findall_cars() {
		List<Cars> list = dao.queryAll();
		ActionContext.getContext().put("list_cars", list);
		ActionContext.getContext().put("msg", "cars");
		return "list_car";
	}
	
	public void querybypage() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if(page==0) {
			page = 1;
		}
		if(limit==0) {
			limit=10;
		}
		PageModel<Cars> pm = dao.queryall(page, limit);
		
		Gson gson = new Gson();
		String json = gson.toJson(pm);
		
		out.print(json);
		out.flush();
		out.close();
	}
	
	
	public String add() {
		dao.save(car);
		return "ok_car";
	}
	
	public String upda() {
		dao.update(car);
		return "ok_car";
	}
	
	public String del() {
		dao.delete(id);
		return "ok_car";
	}
	
	public String get() {
		ActionContext.getContext().put("car", dao.queryById(id));
		ActionContext.getContext().put("msg", "content");
		return "list_car";
	}
	
	public String edit() {
		ActionContext.getContext().put("car", dao.queryById(id));
		return "car";
	}
	
	
	public ICarsService getDao() {
		return dao;
	}
	public void setDao(ICarsService dao) {
		this.dao = dao;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Cars getCar() {
		return car;
	}
	public void setCar(Cars car) {
		this.car = car;
	}

	public PageModel<Cars> getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel<Cars> pageModel) {
		this.pageModel = pageModel;
	}

	public int getpage() {
		return page;
	}

	public void setpage(int page) {
		this.page = page;
	}

	public int getlimit() {
		return limit;
	}

	public void setlimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
	
	
}
